package com.xoa.service.financialManage.contract;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.crm.ProductMapper;
import com.xoa.dao.financialManage.contract.*;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.FlowRunMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.department.Department;
import com.xoa.model.financialManage.contract.*;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowRun;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.sms.SmsService;
import com.xoa.util.*;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jarvishao
 * @site
 * @create 2019-12-24-11:17
 */
@Service
public class ContractManageService {
    @Autowired
    ContractTypesMapper contractTypesMapper;
    @Autowired
    ContractsMapper contractsMapper;
    @Autowired
    ContractFlowsMapper contractFlowsMapper;
    @Autowired
    ContractProjectsMapper contractProjectsMapper;
    @Autowired
    ContractRemindsMapper contractRemindsMapper;
    @Autowired
    ContractTransportsMapper contractTransportsMapper;
    @Resource
    SmsService smsService;
    @Resource
    SysCodeMapper sysCodeMapper;

    @Resource
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    FlowRunMapper flowRunMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    private ContractManageService contractManageService;
    @Resource
    private ProductMapper productMapper;

    @Autowired
    private DepartmentService departmentService;

    //事务提醒 todoId收信人
    @Async
    public void expirationReminder(HttpServletRequest request,final ContractRemind contractRemind,final String todoId) {
        final String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
        final Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        this.threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                ContextHolder.setConsumerType("xoa" + sqlType);
                SmsBody smsBody = new SmsBody();
                smsBody.setFromId(contractRemind.getFollower());
                smsBody.setContent("财务合同管理！主题：" + contractRemind.getContent());
                Date date = new Date();
                long dateLong = date.getTime()/1000;
                String strDate = String.valueOf(dateLong);
                Integer dateInt = Integer.valueOf(strDate.trim());
                smsBody.setSendTime(dateInt);
                smsBody.setIsAttach("0"); //无附件
                smsBody.setRemindUrl("/contract/details?contractId=" + contractRemind.getContractId());

                SysCode sysCode = new SysCode();
                sysCode.setCodeName("合同管理");
                sysCode.setParentNo("SMS_REMIND");
                SysCode codeNoByNameAndParentNo = sysCodeMapper.getCodeNoByNameAndParentNo(sysCode);
                if (codeNoByNameAndParentNo != null) {
                    smsBody.setSmsType(codeNoByNameAndParentNo.getCodeNo());
                }
                String title = user.getUserName() + "：有提醒！";
                String context = "主题:" + contractRemind.getContent();
                smsService.saveSms(smsBody, todoId, "1", "1", title, context, sqlType);
            }
        });
    }

    //开启定时器,每天执行一遍
    public void remindercontract(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map map=new HashMap();
        map.put("remindFlag","0"); //查找未提醒的
        List<ContractRemind> contractReminds = contractRemindsMapper.selectRemindAll(map);
        for (ContractRemind contractRemind: contractReminds) {
            //提醒时间
            long retime = contractRemind.getRemindDate().getTime();
            //当前时间
            Date date = new Date();
            long datime = date.getTime();
            if (datime==retime || datime>retime){
                this.expirationReminder(request,contractRemind,contractRemind.getUserId());

                contractRemind.setRemindFlag(1); //已提醒过
                contractRemindsMapper.updateByPrimaryKey(contractRemind);
            }
        }
    }


    public ToJson<ContractType> insertType(HttpServletRequest request, ContractType contractType) {
        ToJson<ContractType> json = new ToJson<ContractType>(1, "error");
        try{
            int count= contractTypesMapper.insertSelective(contractType);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractType> delType(HttpServletRequest request, Integer[] ids) {
        ToJson<ContractType> json = new ToJson<ContractType>(1, "error");
        try {
            int temp = contractTypesMapper.delTypeByIds(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractType> upType(HttpServletRequest request, ContractType contractType) {
        ToJson<ContractType> json = new ToJson<ContractType>(1, "error");
        try {
            int temp = contractTypesMapper.updateByPrimaryKeySelective(contractType);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<ContractType> selectTypeAll(HttpServletRequest request, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractType> json = new LimsJson<ContractType>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<ContractType> list =  contractTypesMapper.selectTypeAll(map);
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractType> selectTypeById(HttpServletRequest request, Integer typeId) {
        ToJson<ContractType> json = new ToJson<ContractType>(1, "error");
        try{
            ContractType count = contractTypesMapper.selectByPrimaryKey(typeId);
            json.setObject(count);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<ContractType> selectTypeBy(HttpServletRequest request, String cha, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractType> json = new LimsJson<>(1, "error");
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("cha",cha);
        try{
            List<ContractType> list = contractTypesMapper.selectTypeBy(map);
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }
    /*
    新建合同
     */
    public ToJson<Contract> insertContract(HttpServletRequest request, Contracts contracts) {
        ToJson<Contract> json = new ToJson<Contract>(1, "失败");
        try{
            contracts.getContract().setCreateTime(new Date());
            int count= contractsMapper.insertSelective(contracts.getContract());
            Integer contractId = contracts.getContract().getContractId(); //新增完返回主键ID

            //合同流程
            if (null != contracts.getContractFlow()){
                for (ContractFlow contractFlow:contracts.getContractFlow()){//获取合同流程类
                    contractFlow.setConTractId(contractId);
                    if (StringUtils.checkNull(contractFlow.getRunId())){
                        json.setMsg("流程为空");
                        return json;
                    }
                    contractFlowsMapper.insertSelective(contractFlow);
                }
            }

            //结算情况
            if (null != contracts.getContractProject()){
                for (ContractProject contractProject:contracts.getContractProject()){
                    contractProject.setContractId(contractId);

                    if (StringUtils.checkNull(contractProject.getProjectType())){
                        json.setMsg("款项性质不能为空");
                        return json;
                    }
                    if (contractProject.getMoney().equals(null)){
                        json.setMsg("合同金额");
                        return json;
                    }
                    contractProjectsMapper.insertSelective(contractProject);
                }
            }

            //提醒
            if (null != contracts.getContractRemind()){
                for (ContractRemind contractRemind:contracts.getContractRemind()){
                    contractRemind.setContractId(contractId);
                    contractRemind.setRemindFlag(0); //未提醒
                    if (StringUtils.checkNull(contractRemind.getUserId())){
                        json.setMsg("提醒用户不能为空");
                        return json;
                    }
                    if (null==contractRemind.getRemindDate()){
                        json.setMsg("提醒日期不能为空");
                        return json;
                    }
                    if (StringUtils.checkNull(contractRemind.getContent())){
                        json.setMsg("提醒内容不能为空");
                        return json;
                    }
                    contractRemindsMapper.insertSelective(contractRemind);
                }
            }

            //订单
            if (null != contracts.getContractTransport()){
                for (ContractTransport contractTransport:contracts.getContractTransport()){
                    contractTransport.setContractId(contractId);
                    if (null==contractTransport.getProductId()){
                        json.setMsg("产品不能为空");
                        return json;
                    }
                    if (null==contractTransport.getShippingDate()){
                        json.setMsg("发货时间不能为空");
                        return json;
                    }
                    if (StringUtils.checkNull(contractTransport.getNumber())){
                        json.setMsg("发货数量不能为空");
                        return json;
                    }
                    contractTransportsMapper.insertSelective(contractTransport);
                }
            }


            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<Contract> selectContractByNo(HttpServletRequest request, String contractNo) {//查询合同编号是否重复
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try{
            int count = contractsMapper.selectContractByNo(contractNo);
            if(count>0){
                json.setMsg("该合同编号已存在");
                json.setFlag(1);
            }else {
                json.setMsg("该合同编号可以使用");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<Contract> delContract(HttpServletRequest request, Integer[] ids) {
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try {
            int temp = contractsMapper.delContract(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<Contract> upContract(HttpServletRequest request, Contract contract) {
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try {
            int temp = contractsMapper.updateByPrimaryKeySelective(contract);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<Contract> selectContractAll(HttpServletRequest request, Integer page, Integer limit,boolean useFlag) {
        LimsJson<Contract> json = new LimsJson<Contract>(1,"false");
        Map map = new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(useFlag);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }

        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);

            if (users.getUserPriv() != 1){
                map.put("deptId",users.getDeptId());
            }
            //类型
            map.put("typeId",request.getParameter("typeId"));
            //种类
            map.put("contractType",request.getParameter("contractType"));
            //金额
            map.put("money",request.getParameter("money"));
            map.put("moneyEnd",request.getParameter("moneyEnd"));
            //标题
            map.put("title",request.getParameter("title"));

            List<Contract> list =  contractsMapper.selectContractAll(map);

            BigDecimal summoney=new BigDecimal("0");
            for (Contract list1:list){

                //合同类型
                if (list1.getTypeId() != null){
                    ContractType contractType = contractTypesMapper.selectByPrimaryKey(list1.getTypeId());
                    list1.setTypeName(contractType !=null ? contractType.getTypeName() : "");
                }

                //合同种类
                if (!StringUtils.checkNull(list1.getContract_type())){
                    SysCode getSingleCode = sysCodeMapper.getSingleCode("CONTRACT_TYPE",list1.getContract_type());
                    if (getSingleCode != null) {
                        list1.setContractTypeName(getSingleCode.getCodeName());
                    }
                }

                //签订时间
                if (list1.getDate_comment() != null){
                    list1.setDateComment(DateFormat.getDatestr(list1.getDate_comment()));
                }else{
                    list1.setDateComment("");
                }

                //授权代表
                if (!StringUtils.checkNull(list1.getAuthorized_representative())){
                    list1.setAuthorizedRepresentative(list1.getAuthorized_representative());
                }else{
                    list1.setAuthorizedRepresentative("");
                }
                //开始时间
                if (list1.getDuration_startdate() != null ){
                    list1.setCloneDurationStartdate(DateFormat.getDatestr(list1.getDuration_startdate()));
                }else{
                    list1.setCloneDurationStartdate("");
                }
                //终止时间
                if (list1.getDuration_enddate() != null ){
                    list1.setCloneDurationEnddate(DateFormat.getDatestr(list1.getDuration_enddate()));
                }else{
                    list1.setCloneDurationEnddate("");
                }

                //总金额
                summoney = summoney.add(list1.getMoney());
            }

            json.setObject(summoney); //返回总金额
            json.setData(list);
            PageParams pageParams =(PageParams) map.get("pageParams");
            json.setCount(pageParams.getTotal());
            json.setMsg("ok");
            json.setCode(0);
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    public LimsJson<Contract> selectContractMy(HttpServletRequest request, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<Contract> json = new LimsJson<Contract>();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users =  SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        String userId=users.getUserId();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("userId",userId);
        try {
            List<Contract> list =  contractsMapper.selectContractMy(map);
            for (Contract list1:list){
                try {
                    list1.setTypeName(contractTypesMapper.selectByPrimaryKey(list1.getTypeId()).getTypeName());
                }catch (Exception e){
                    json.setMsg(e.getMessage());
                    e.printStackTrace();
                }
                if (list1.getParentId()!=0) {
                    list1.setParentName(contractsMapper.selectContractById(list1.getParentId()).getTitle());
                }
                if (null!=list1.getUserId()) {
                    list1.setUserName(usersMapper.getUsernameByUserId(list1.getUserId()));
                }
                try {
                    if (StringUtils.checkNull(list1.getSubContract())) {
                        List<Contract> list2 = new ArrayList<>();
                        String sub = contractsMapper.selectContractZiById(list1.getContractId());
                        if (!StringUtils.checkNull(sub)) {
                            String[] subsArray = sub.split(",");
                            for (String contract : subsArray) {
                                list.add(contractsMapper.selectContractById(Integer.parseInt(contract)));
                            }
                        }
                        list1.setSubContracts(list2);
                    }
                }catch (Exception e){
                    json.setMsg(e.getMessage());
                    e.printStackTrace();
                }
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    public ToJson<Contract> selectContractById(HttpServletRequest request, Integer contractId) {
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try{
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);


            Contract count = contractsMapper.selectContractById(contractId);
            if (count != null){

                //所属部门
                if (count.getDept_id() != null){
                    Department deptById = departmentService.getDeptById(count.getDept_id());
                    count.setDeptName(deptById.getDeptName());
                }

                //合同类型
                if (count.getTypeId()!=null){
                    ContractType contractType = contractTypesMapper.selectByPrimaryKey(count.getTypeId());
                    count.setTypeName(contractType !=null ? contractType.getTypeName() : "");
                }

                //子合同
                List<Contract> list = new ArrayList<>();
                String sub = contractsMapper.selectContractZiById(contractId);
                if (!StringUtils.checkNull(sub)) {
                    String[] subsArray = sub.split(",");
                    for (String contract : subsArray) {
                        list.add(contractsMapper.selectContractById(Integer.parseInt(contract)));
                    }
                }
                count.setSubContracts(list);

                //主合同
                if (count.getParentId()!=0) {
                    Contract contract = contractsMapper.selectContractById(count.getParentId());
                    count.setParentName(contract != null ? contract.getTitle() : "");
                }

                //合同附件
                if (!StringUtils.checkNull(count.getAttachmentName()) && !StringUtils.checkNull(count.getAttachmentId())){
                    count.setAttachment(GetAttachmentListUtil.returnAttachment(count.getAttachmentName(), count.getAttachmentId(), "xoa"+loginDateSouse, GetAttachmentListUtil.CONTRACT));
                }

                //合同种类
                if (!StringUtils.checkNull(count.getContract_type())){
                    SysCode getSingleCode = sysCodeMapper.getSingleCode("CONTRACT_TYPE",count.getContract_type());
                    if (getSingleCode != null) {
                        count.setContractTypeName(getSingleCode.getCodeName());
                    }
                }

            }

            json.setObject(count);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }
    public ToJson<Contract> upContractUserId(HttpServletRequest request, Integer contractId,String userId) {
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try {
            int temp = contractsMapper.upContractUserId(contractId,userId);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /*
    结算情况（项目）
     */
    public ToJson<ContractProject> insertProject(HttpServletRequest request, ContractProject contractProject) {
        ToJson<ContractProject> json = new ToJson<ContractProject>(1, "error");
        try{
            int count= contractProjectsMapper.insertSelective(contractProject);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractProject> delProject(HttpServletRequest request, Integer[] ids) {
        ToJson<ContractProject> json = new ToJson<ContractProject>(1, "error");
        try {
            int temp = contractProjectsMapper.delProjectByIds(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractProject> upProject(HttpServletRequest request, ContractProject contractProject) {
        ToJson<ContractProject> json = new ToJson<ContractProject>(1, "error");
        try {
            int temp = contractProjectsMapper.updateByPrimaryKeySelective(contractProject);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<ContractProject> selectProjectAll(HttpServletRequest request, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractProject> json = new LimsJson<ContractProject>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<ContractProject> list =  contractProjectsMapper.selectProjectAll(map);
            for (ContractProject list1:list){
                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(list1.getRunId())) {
                    String[] runIdsArray = list1.getRunId().split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                //项目类别
                list1.setProjectTypeId(Integer.parseInt(list1.getProjectType()));////0收入  1支出
                String status = list1.getProjectType();
                if (status.equals("0")){//0收入  1支出
                    list1.setProjectType("收入");
                }else if(status.equals("1")){
                    list1.setProjectType("支出");
                }
                //付款方式
                list1.setPayWayId(list1.getPayWay());
                String payWay = list1.getPayWay();
                if (payWay.equals("01")){
                    list1.setPayWay("现金");
                }else if (payWay.equals("02")){
                    list1.setPayWay("转账");
                }else if (payWay.equals("03")){
                    list1.setPayWay("支票");
                }else if (payWay.equals("04")){
                    list1.setPayWay("支付宝");
                }else if (payWay.equals("05")){
                    list1.setPayWay("微信");
                }else if (payWay.equals("06")){
                    list1.setPayWay("其他");
                }

            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractProject> selectProjectById(HttpServletRequest request, Integer conProjectId) {
        ToJson<ContractProject> json = new ToJson<ContractProject>(1, "error");
        try{
            ContractProject list1 = contractProjectsMapper.selectByPrimaryKey(conProjectId);
            List<FlowRun> runIdnames = new ArrayList<FlowRun>();
            if (!StringUtils.checkNull(list1.getRunId())) {
                String[] runIdsArray = list1.getRunId().split(",");
                for (String runId : runIdsArray) {
                    FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                    runIdnames.add(flow);
                }
            }
            list1.setRunIds(runIdnames);
            //项目类别
            list1.setProjectTypeId(Integer.parseInt(list1.getProjectType()));////0收入  1支出
            String status = list1.getProjectType();
            if (status.equals("0")){//0收入  1支出
                list1.setProjectType("收入");
            }else if(status.equals("1")){
                list1.setProjectType("支出");
            }
            //付款方式
            list1.setPayWayId(list1.getPayWay());
            String payWay = list1.getPayWay();
            if (payWay.equals("01")){
                list1.setPayWay("现金");
            }else if (payWay.equals("02")){
                list1.setPayWay("转账");
            }else if (payWay.equals("03")){
                list1.setPayWay("支票");
            }else if (payWay.equals("04")){
                list1.setPayWay("支付宝");
            }else if (payWay.equals("05")){
                list1.setPayWay("微信");
            }else if (payWay.equals("06")){
                list1.setPayWay("其他");
            }

            json.setObject(list1);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    /*
    订单（物流运输）crud
     */
    public ToJson<ContractTransport> insertTransport(HttpServletRequest request, ContractTransport contractTransport) {
        ToJson<ContractTransport> json = new ToJson<ContractTransport>(1, "error");
        try{
            int count= contractTransportsMapper.insertSelective(contractTransport);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractTransport> delTransport(HttpServletRequest request, Integer[] ids) {
        ToJson<ContractTransport> json = new ToJson<ContractTransport>(1, "error");
        try {
            int temp = contractTransportsMapper.delTransportByIds(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractTransport> upTransport(HttpServletRequest request, ContractTransport contractTransport) {
        ToJson<ContractTransport> json = new ToJson<ContractTransport>(1, "error");
        try {
            int temp = contractTransportsMapper.updateByPrimaryKeySelective(contractTransport);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<ContractTransport> selectTransportAll(HttpServletRequest request, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractTransport> json = new LimsJson<ContractTransport>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<ContractTransport> list =  contractTransportsMapper.selectTransportAll(map);
            for (ContractTransport list1:list){
                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(list1.getRunId())) {
                    String[] runIdsArray = list1.getRunId().split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                list1.setRunIds(runIdnames);
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractTransport> selectTransportById(HttpServletRequest request, Integer transportId) {
        ToJson<ContractTransport> json = new ToJson<ContractTransport>(1, "error");
        try{
            ContractTransport list1 = contractTransportsMapper.selectByPrimaryKey(transportId);
            List<FlowRun> runIdnames = new ArrayList<FlowRun>();
            if (!StringUtils.checkNull(list1.getRunId())) {
                String[] runIdsArray = list1.getRunId().split(",");
                for (String runId : runIdsArray) {
                    FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                    runIdnames.add(flow);
                }
            }
            list1.setRunIds(runIdnames);
            json.setObject(list1);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }
    /*
    相关流程crud
     */
    public ToJson<ContractFlow> insertFlow(HttpServletRequest request, ContractFlow contractFlow) {
        ToJson<ContractFlow> json = new ToJson<ContractFlow>(1, "error");
        try{
            int count= contractFlowsMapper.insertSelective(contractFlow);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractFlow> delFlow(HttpServletRequest request, Integer[] ids) {
        ToJson<ContractFlow> json = new ToJson<ContractFlow>(1, "error");
        try {
            int temp = contractFlowsMapper.delFlowByIds(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractFlow> upFlow(HttpServletRequest request, ContractFlow contractFlow) {
        ToJson<ContractFlow> json = new ToJson<ContractFlow>(1, "error");
        try {
            int temp = contractFlowsMapper.updateByPrimaryKeySelective(contractFlow);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<ContractFlow> selectFlowAll(HttpServletRequest request, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractFlow> json = new LimsJson<ContractFlow>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<ContractFlow> list =  contractFlowsMapper.selectFlowAll(map);
            for (ContractFlow list1:list){
                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(list1.getRunId())) {
                    String[] runIdsArray = list1.getRunId().split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                list1.setRunIds(runIdnames);
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractFlow> selectFlowById(HttpServletRequest request, Integer conFlowId) {
        ToJson<ContractFlow> json = new ToJson<ContractFlow>(1, "error");
        try{
            ContractFlow list1 = contractFlowsMapper.selectByPrimaryKey(conFlowId);
            List<FlowRun> runIdnames = new ArrayList<FlowRun>();
            if (!StringUtils.checkNull(list1.getRunId())) {
                String[] runIdsArray = list1.getRunId().split(",");
                for (String runId : runIdsArray) {
                    FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                    runIdnames.add(flow);
                }
            }
            list1.setRunIds(runIdnames);
            json.setObject(list1);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }
    /*
    相关流程crud
     */
    public ToJson<ContractRemind> insertRemind(HttpServletRequest request, ContractRemind contractRemind) {
        ToJson<ContractRemind> json = new ToJson<ContractRemind>(1, "error");
        try{
            int count= contractRemindsMapper.insertSelective(contractRemind);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractRemind> delRemind(HttpServletRequest request, Integer[] ids) {
        ToJson<ContractRemind> json = new ToJson<ContractRemind>(1, "error");
        try {
            int temp = contractRemindsMapper.delRemindByIds(ids);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractRemind> upRemind(HttpServletRequest request, ContractRemind contractRemind) {
        ToJson<ContractRemind> json = new ToJson<ContractRemind>(1, "error");
        try {
            int temp = contractRemindsMapper.updateByPrimaryKeySelective(contractRemind);
            if (temp > 0) {
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<ContractRemind> selectRemindAll(HttpServletRequest request, Integer page, Integer limit,Integer pageSize, Map map) {
        LimsJson<ContractRemind> json = new LimsJson<ContractRemind>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<ContractRemind> list =  contractRemindsMapper.selectRemindAll(map);
            for (ContractRemind list1:list){
                if (null!=list1.getUserId()) {
                    list1.setUserName(usersMapper.getUsernameByUserId(list1.getUserId()));
                }
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public ToJson<ContractRemind> selectRemindById(HttpServletRequest request, Integer remindId) {
        ToJson<ContractRemind> json = new ToJson<ContractRemind>(1, "error");
        try{
            ContractRemind list1 = contractRemindsMapper.selectByPrimaryKey(remindId);
            if (null!=list1.getUserId()) {
                list1.setUserName(usersMapper.getUsernameByUserId(list1.getUserId()));
            }
            json.setObject(list1);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<Contract> imports(HttpServletRequest request, MultipartFile file) throws IOException {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
        ToJson toJson = new ToJson(1,"error");
        try {
            // 判断是否为空文件
            if (file.isEmpty()) {
                toJson.setMsg("请上传文件！");
                toJson.setFlag(1);
                return toJson;
            }
            String fileName = file.getOriginalFilename();
            if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                InputStream in = file.getInputStream();
                HSSFWorkbook hssfWorkbook = new HSSFWorkbook(in);
                HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
                List listMsg = new ArrayList();
                for (int rowNum = 2; rowNum < hssfSheet.getPhysicalNumberOfRows(); rowNum++) {
                    HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                    if (hssfRow != null){
                        Map mapMsg = new HashMap();
                        Contract contract=new Contract();
                        //跟进人
                        contract.setUserId(users.getUserId());
                        for ( int i = 0; i <  hssfRow.getPhysicalNumberOfCells(); i++ ) {
                            HSSFCell hssfcell = hssfRow.getCell(i);

                            String cell = "";
                            if (hssfcell != null){
                                hssfcell.setCellType(CellType.STRING);
                                cell =hssfcell.getStringCellValue();
                            }

                            switch (i){
                                case 0:
                                    //合同编号 CONTRACT_NO
                                    if(!StringUtils.checkNull(cell)){
                                        contract.setContractNo(cell);
                                    }else{
                                        mapMsg.put("rowNum",rowNum+1);
                                        mapMsg.put("hssfRow",i+1);
                                        mapMsg.put("toMsg","合同编号为空");
                                        continue;
                                    }
                                    break;
                                case 1:
                                    //合同名称 TITLE
                                    contract.setTitle(cell);
                                    break;
                                case 2:
                                    //客户名称
                                    contract.setTargetName(cell);
                                    break;
                                case 3:
                                    //签订时间
                                    if(!StringUtils.checkNull(cell)){
                                        contract.setDate_comment(DateFormat.getDates(cell));
                                    }
                                    break;
                                case 4:
                                    //授权代表
                                    contract.setAuthorized_representative(cell);
                                    break;
                                case 5:
                                    //合同工期 开始时间
                                    if(!StringUtils.checkNull(cell)){
                                        contract.setDuration_startdate(DateFormat.getDates(cell));
                                    }
                                    break;
                                case 6:
                                    //合同工期 终止时间
                                    if(!StringUtils.checkNull(cell)){
                                        contract.setDuration_enddate(DateFormat.getDates(cell));
                                    }
                                    break;
                                case 7:
                                    // 执行单位
                                    contract.setExecutive_unit(cell);
                                    break;
                                case 8:
                                    //合同金额
                                    contract.setMoney(new BigDecimal(cell));
                                    break;
                                case 9:
                                    // 付款方式
                                    contract.setPayment(cell);
                                    break;
                                case 10:
                                    //合同评审（是，否）
                                    if (cell.equals("是")){
                                        contract.setContract_approval("1");
                                    }else{
                                        contract.setContract_approval("0");
                                    }
                                    break;
                                case 11:
                                    //风险评估 （是，否）
                                    if (cell.equals("否")){
                                        contract.setContract_approval("0");
                                    }else{
                                        contract.setContract_approval("1");
                                    }
                                    break;
                                case 12:
                                    // 备注
                                    contract.setRemarks(cell);
                                    break;
                                case 13:
                                    //合同种类
                                    if(cell.equals("普通合同")){
                                        contract.setContract_type("01");
                                    }else if(cell.equals("重大合同")){
                                        contract.setContract_type("02");
                                    }else {
                                        contract.setContract_type("03");
                                    }
                                    break;
                                case 14:
                                    //合同类型
                                    ContractType contractType = contractTypesMapper.selectTypeByName(cell);
                                    if (contractType != null){
                                        contract.setTypeId(contractType.getTypeId());
                                    }
                                    break;
                            }
                        }
                        contractsMapper.insertSelective(contract);
                        if (mapMsg.size()>0){
                            listMsg.add(mapMsg);
                        }
                    }
                }
                toJson.setObj(listMsg);
                toJson.setMsg("导入成功");
                toJson.setFlag(0);
            }else{
                toJson.setMsg("请导入.xls或.xlsx格式的文件");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    public LimsJson<Contract> selectContractBy(HttpServletRequest request, String cha, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<Contract> json = new LimsJson<>(1, "error");
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("cha",cha);
        try{
            List<Contract> list = contractsMapper.selectContractBy(map);
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<Contract> selectContractByType(HttpServletRequest request, String type, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<Contract> json = new LimsJson<>(1, "暂无数据");
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("type",type);
        try{
            List<Contract> list = contractsMapper.selectContractByType(map);
            for (Contract list1:list){
                if (null!=list1.getTypeId()) {
                    list1.setTypeName(contractTypesMapper.selectByPrimaryKey(list1.getTypeId()).getTypeName());
                }
                if (null!=list1.getUserId()) {
                    list1.setUserName(usersMapper.getUsernameByUserId(list1.getUserId()));
                }
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<FlowRun> selectFlow(HttpServletRequest request, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<FlowRun> json = new LimsJson<FlowRun>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<FlowRun> list =  flowRunMapper.selectFlow(map);
            FlowRun data[] = new FlowRun[list.size()];
            int i = 0;
            for (FlowRun list1:list){
                String username = usersMapper.getUsernameByUserId(list1.getBeginUser());
                list1.setUserName(username);
                data[i] = list1;
                i++;
            }
            if (list.size() > 0) {
                json.setData(list);
                json.setObj(data);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<FlowRun> selectContractFlowById(HttpServletRequest request, Integer contractId) {
        LimsJson<FlowRun> json = new LimsJson<FlowRun>(1, "error");
        try{
            List<ContractFlow> list = contractFlowsMapper.selectContractById(contractId);
            int i = 0;
            for (ContractFlow list1:list){
                String runIds = list1.getRunId();//查询关联的流程
                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(runIds)) {
                    String[] runIdsArray = runIds.split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow= flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                list1.setRunIds(runIdnames);
            }
            json.setObj(list);
            json.setMsg("ok");
            json.setCode(0);
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<ContractTransport> selectContractTransportById(HttpServletRequest request, Integer contractId) {
        LimsJson<ContractTransport> json = new LimsJson<>(1, "error");
        try{
            List<ContractTransport> count = contractTransportsMapper.selectContractById(contractId);
            for (ContractTransport contractTransport:count) {
                //关联流程
                String runIds = contractTransport.getRunId();//查询关联的流程

                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(runIds)) {
                    String[] runIdsArray = runIds.split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                contractTransport.setRunIds(runIdnames);
                contractTransport.setProductName(productMapper.selectByPrimaryKey(contractTransport.getProductId()).getProductName());
            }
            json.setObject(count);
            json.setMsg("ok");
            json.setCode(0);
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public ToJson<ContractRemind> selectContractRemindById(HttpServletRequest request, Integer contractId) {
        ToJson<ContractRemind> json = new ToJson<ContractRemind>(1, "error");
        try{
            List<ContractRemind> count = contractRemindsMapper.selectContractById(contractId);
            for (ContractRemind list:count){
                if (!StringUtils.checkNull(list.getUserId())) {
                    String[] usersArray = list.getUserId().split(",");
                    for (String users : usersArray) {
                        /*FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);*/
                        String username = usersMapper.getUsernameByUserId(users);//跟进人姓名
                        list.setUserName(username);
                    }
                }
            }
            json.setObject(count);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<ContractProject> selectContractProjectById(HttpServletRequest request, Integer contractId,String Flag) {
        LimsJson<ContractProject> json = new LimsJson<>(1, "error");
        try{
            List<ContractProject> contractProjects = contractProjectsMapper.selectContractById(contractId);
            List<ContractProject> contractProjectZi = contractProjectsMapper.selectContractZiById(contractId);//查询子合同
            List<ContractProject> list1 = new ArrayList<>();
            for (ContractProject Project : contractProjects) {

                //项目类别
                String status = Project.getProjectType();
                if (!StringUtils.checkNull(status)){
                    Project.setProjectTypeId(Integer.parseInt(status));////0收入  1支出
                    if (status.equals("0")){//0收入  1支出
                        Project.setProjectType("收入");
                    }else if(status.equals("1")){
                        Project.setProjectType("支出");
                    }
                }

                //付款方式
                Project.setPayWayId(Project.getPayWay());
                String payWay = Project.getPayWay();
                if (!StringUtils.checkNull(payWay)){
                    if (payWay.equals("01")){
                        Project.setPayWay("现金");
                    }else if (payWay.equals("02")){
                        Project.setPayWay("转账");
                    }else if (payWay.equals("03")){
                        Project.setPayWay("支票");
                    }else if (payWay.equals("04")){
                        Project.setPayWay("支付宝");
                    }else if (payWay.equals("05")){
                        Project.setPayWay("微信");
                    }else if (payWay.equals("06")){
                        Project.setPayWay("其他");
                    }
                }

                //标题
                Contract count = contractsMapper.selectContractById(contractId);
                Project.setTitle(count.getTitle());
                //关联流程
                String runIds = Project.getRunId();//查询关联的流程

                List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                if (!StringUtils.checkNull(runIds)) {
                    String[] runIdsArray = runIds.split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                Project.setRunIds(runIdnames);

                if (Project.getInvoiceTime()!=null){
                    Project.setInvoiceTimeStr(DateFormat.getStrDate(Project.getInvoiceTime()));
                }

                if (Project.getPayTime()!=null){
                    Project.setPayTimeStr(DateFormat.getStrDate(Project.getPayTime()));

                }
                list1.add(Project);
            }
            if ("1".equals(Flag)){
                for (ContractProject Projects : contractProjectZi) {

                    //项目类别
                    Projects.setProjectTypeId(Integer.parseInt(Projects.getProjectType()));////0收入  1支出
                    String status = Projects.getProjectType();
                    if (status.equals("0")){//0收入  1支出
                        Projects.setProjectType("收入");
                    }else if(status.equals("1")){
                        Projects.setProjectType("支出");
                    }

                    //付款方式
                    Projects.setPayWayId(Projects.getPayWay());
                    String payWay = Projects.getPayWay();
                    if (payWay.equals("01")){
                        Projects.setPayWay("现金");
                    }else if (payWay.equals("02")){
                        Projects.setPayWay("转账");
                    }else if (payWay.equals("03")){
                        Projects.setPayWay("支票");
                    }else if (payWay.equals("04")){
                        Projects.setPayWay("支付宝");
                    }else if (payWay.equals("05")){
                        Projects.setPayWay("微信");
                    }else if (payWay.equals("06")){
                        Projects.setPayWay("其他");
                    }
                    //标题
                    Contract count = contractsMapper.selectContractById(contractId);
                    Projects.setTitle(count.getTitle());
                    //关联流程
                    String runIds = Projects.getRunId();//查询关联的流程

                    List<FlowRun> runIdnames = new ArrayList<FlowRun>();
                    if (!StringUtils.checkNull(runIds)) {
                        String[] runIdsArray = runIds.split(",");
                        for (String runId : runIdsArray) {
                            FlowRun flow = flowRunMapper.selectByRunId(Integer.parseInt(runId));
                            runIdnames.add(flow);
                        }
                    }
                    Projects.setRunIds(runIdnames);
                    Projects.setInvoiceTimeStr(DateFormat.getStrDate(Projects.getInvoiceTime()));

                    Projects.setPayTimeStr(DateFormat.getStrDate(Projects.getPayTime()));


                    list1.add(Projects);

                }
            }
            json.setObject(list1);
            json.setMsg("ok");
            json.setCode(0);
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<Contract> selectContractZi(HttpServletRequest request, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<Contract> json = new LimsJson<Contract>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try {
            List<Contract> list =  contractsMapper.selectContractZi(map);
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    public LimsJson<Contract> selectContractZiBy(HttpServletRequest request, String cha, Integer page, Integer limit, Integer pageSize, Map map) {
        LimsJson<Contract> json = new LimsJson<>(1, "error");
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("cha",cha);
        try{
            List<Contract> list = contractsMapper.selectContractZiBy(map);
            if (list.size() > 0) {
                json.setData(list);
                json.setCount(list.size());
                json.setMsg("ok");
                json.setCode(0);
            }
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<Contract> selectContractZiById(HttpServletRequest request, Integer contractId) {
        LimsJson<Contract> json = new LimsJson<Contract>(1, "error");
        try{
            List<Contract> list = new ArrayList<>();
            String sub = contractsMapper.selectContractZiById(contractId);
            if (!StringUtils.checkNull(sub)) {
                String[] subsArray = sub.split(",");
                for (String contract : subsArray) {
                    list.add(contractsMapper.selectContractById(Integer.parseInt(contract)));
                }
            }

            json.setObject(list);
            json.setData(list);
            json.setMsg("ok");
            json.setCode(0);
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<ContractProject> exportProject(HttpServletRequest request, HttpServletResponse response,Integer contractId,String Flag) {
        LimsJson<ContractProject> json = new LimsJson<ContractProject>();

        try {//Flag （0不包含子合同，1包含子合同）
            ContractProject contractProject = new ContractProject();
            List<ContractProject> contractProjects = contractProjectsMapper.selectContractById(contractId);
            List<ContractProject> contractProjectZi = contractProjectsMapper.selectContractZiById(contractId);//查询子合同
            List<ContractProject> list1 = new ArrayList<>();
            for (ContractProject Project : contractProjects) {

                //项目类别
                String status = Project.getProjectType();
                if (status.equals("0")){//0收入  1支出
                    Project.setProjectType("收入");
                }else if(status.equals("1")){
                    Project.setProjectType("支出");
                }

                //付款方式
                String payWay = Project.getPayWay();
                if (payWay.equals("01")){
                    Project.setPayWay("现金");
                }else if (payWay.equals("02")){
                    Project.setPayWay("转账");
                }else if (payWay.equals("03")){
                    Project.setPayWay("支票");
                }else if (payWay.equals("04")){
                    Project.setPayWay("支付宝");
                }else if (payWay.equals("05")){
                    Project.setPayWay("微信");
                }else if (payWay.equals("06")){
                    Project.setPayWay("其他");
                }
                //标题
                Contract count = contractsMapper.selectContractById(contractId);
                Project.setTitle(count.getTitle());
                //关联流程
                String runIds = Project.getRunId();//查询关联的流程

                List<FlowRun> runIdnames = new ArrayList<FlowRun>();

                if (!StringUtils.checkNull(runIds)) {
                    String[] runIdsArray = runIds.split(",");
                    for (String runId : runIdsArray) {
                        FlowRun flow= flowRunMapper.selectByRunId(Integer.parseInt(runId));
                        runIdnames.add(flow);
                    }
                }
                Project.setRunIds(runIdnames);
                String runnames="";
                for (FlowRun runNames:runIdnames){
                    Project.setRunName(runNames.getRunName());
                }



                Project.setInvoiceTimeStr(DateFormat.getStrDate(Project.getInvoiceTime()));

                Project.setPayTimeStr(DateFormat.getStrDate(Project.getPayTime()));


                list1.add(Project);

            }
            if ("1".equals(Flag)){
                for (ContractProject Projects : contractProjectZi) {

                    //项目类别
                    String status = Projects.getProjectType();
                    if (status.equals("0")){//0收入  1支出
                        Projects.setProjectType("收入");
                    }else if(status.equals("1")){
                        Projects.setProjectType("支出");
                    }

                    //付款方式
                    String payWay = Projects.getPayWay();
                    if (payWay.equals("01")){
                        Projects.setPayWay("现金");
                    }else if (payWay.equals("02")){
                        Projects.setPayWay("转账");
                    }else if (payWay.equals("03")){
                        Projects.setPayWay("支票");
                    }else if (payWay.equals("04")){
                        Projects.setPayWay("支付宝");
                    }else if (payWay.equals("05")){
                        Projects.setPayWay("微信");
                    }else if (payWay.equals("06")){
                        Projects.setPayWay("其他");
                    }
                    //标题
                    Contract count = contractsMapper.selectContractById(contractId);
                    Projects.setTitle(count.getTitle());
                    //关联流程
                    String runIds = Projects.getRunId();//查询关联的流程

                    List<FlowRun> runIdnames = new ArrayList<FlowRun>();

                    if (!StringUtils.checkNull(runIds)) {
                        String[] runIdsArray = runIds.split(",");
                        for (String runId : runIdsArray) {
                            FlowRun flow= flowRunMapper.selectByRunId(Integer.parseInt(runId));
                            runIdnames.add(flow);
                        }
                    }
                    Projects.setRunIds(runIdnames);
                    String runnames="";
                    for (FlowRun runNames:runIdnames){
                        Projects.setRunName(runNames.getRunName());
                    }
                    Projects.setInvoiceTimeStr(DateFormat.getStrDate(Projects.getInvoiceTime()));

                    Projects.setPayTimeStr(DateFormat.getStrDate(Projects.getPayTime()));


                    list1.add(Projects);

                }
            }

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("项目详情表", 12);
            String[] secondTitles = {"序号","标题", "款项性质", "款项类别", "款项金额（元）","打款方式","打款账号","打款时间","开票时间","关联流程名称","备注"};



            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            String[] beanProperty = { "conProjectId","title" ,"projectType", "payType","money","payWay","payAccount","payTimeStr","invoiceTimeStr","runName","remarks"};


            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, list1, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "项目详情表.xls";
            filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename=" + filename);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(1);
        }
        return json;
    }

    public LimsJson<FlowRun> selectFlowBy(HttpServletRequest request, Integer page, Integer limit, Integer pageSize, String cha, Map map) {
        LimsJson<FlowRun> json = new LimsJson<FlowRun>(1, "error");
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("cha",cha);
        try{
            List<FlowRun> list = flowRunMapper.selectFlowBy(map);

            json.setData(list);
            json.setMsg("ok");
            json.setCode(0);
        }catch(Exception e){
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    public LimsJson<ContractProject> projectStatistics(HttpServletRequest request, HttpServletResponse response, Integer contractId, String Flag) {
        LimsJson<ContractProject> json = new LimsJson<ContractProject>();

        try {
            ContractProject contractProject = new ContractProject();
            List[] data = new List[3];
            List<ContractProject> contractProjects = contractProjectsMapper.selectContractById(contractId);
            List<ContractProject> contractProjectZi = contractProjectsMapper.selectContractZiById(contractId);//查询子合同

            List list = new ArrayList<>();//zong
            List<ContractProject> list1 = new ArrayList<>();//0收入
            List<ContractProject> list2 = new ArrayList<>();//1支出
            List list3 = new ArrayList<>();//结算情况
            for (ContractProject Project : contractProjects) {
                if ("0".equals(Project.getProjectType())){

                    String status = Project.getProjectType();//收入还是支出
                    if (status.equals("0")){//0收入  1支出
                        Project.setProjectType("收入");
                    }else if(status.equals("1")){
                        Project.setProjectType("支出");
                    }
                    //标题
                    Contract count = contractsMapper.selectContractById(contractId);
                    Project.setTitle(count.getTitle());
                    list1.add(Project);
                }
                if ("1".equals(Project.getProjectType())){

                    String status = Project.getProjectType();//收入还是支出
                    if (status.equals("0")){//0收入  1支出
                        Project.setProjectType("收入");
                    }else if(status.equals("1")){
                        Project.setProjectType("支出");
                    }
                    //标题
                    Contract count = contractsMapper.selectContractById(contractId);
                    Project.setTitle(count.getTitle());
                    list2.add(Project);
                }
            }
            if ("1".equals(Flag)){
                for (ContractProject Project : contractProjectZi) {
                    if ("0".equals(Project.getProjectType())){

                        String status = Project.getProjectType();//收入还是支出
                        if (status.equals("0")){//0收入  1支出
                            Project.setProjectType("收入");
                        }else if(status.equals("1")){
                            Project.setProjectType("支出");
                        }
                        //标题
                        Contract count = contractsMapper.selectContractById(contractId);
                        Project.setTitle(count.getTitle());
                        list1.add(Project);
                    }
                    if ("1".equals(Project.getProjectType())){

                        String status = Project.getProjectType();//收入还是支出
                        if (status.equals("0")){//0收入  1支出
                            Project.setProjectType("收入");
                        }else if(status.equals("1")){
                            Project.setProjectType("支出");
                        }
                        //标题
                        Contract count = contractsMapper.selectContractById(contractId);
                        Project.setTitle(count.getTitle());
                        list2.add(Project);
                    }
                }
            }
            data[0]=list1;//收入
            data[1]=list2;//支出
            ContractProject zongs= new ContractProject();
            BigDecimal shou = new BigDecimal("0.00");
            for (ContractProject shous:list1){
                shou = shou.add(shous.getMoney());
            }
            BigDecimal chu = new BigDecimal("0.00");
            for (ContractProject chus:list2){
                chu = chu.subtract(chus.getMoney());
            }
            BigDecimal zong = new BigDecimal("0.00");
            zong = shou.add(chu);

            zongs.setShou(shou);
            zongs.setChu(chu);
            zongs.setZong(zong);
            zongs.setTitle(list1.get(0).getTitle());

            list3.add(zongs);
            data[2]=list3;//统计结算

            list.add(list1);
            list.add(list2);
            list.add(list3);

            json.setData(list);
            json.setObj(data);
            json.setMsg("ok");
            json.setCode(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    //合同导出
    public LimsJson<Contract> exportContract(HttpServletRequest request,HttpServletResponse response, Integer page, Integer limit, Integer pageSize, Map map,String contractType) {
        LimsJson<Contract> json = new LimsJson<Contract>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("contractType",contractType);
        try {
            List<Contract> list =  contractsMapper.selectContract(map);
            for (Contract list1:list){

                //签订时间
                if (list1.getaSignTime()!=null){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
                    list1.setCloneASignTime(simpleDateFormat.format(list1.getaSignTime()));
                }

                //开始时间
                if (list1.getDuration_startdate()!=null){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
                    list1.setCloneDurationStartdate(simpleDateFormat.format(list1.getDuration_startdate()));
                }

                //终止时间
                if (list1.getDuration_enddate()!=null){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
                    list1.setCloneDurationEnddate(simpleDateFormat.format(list1.getDuration_enddate()));
                }

                //合同评审
                if (list1.getContract_approval()!=null){
                    if(list1.getContract_approval().equals("1")){
                        list1.setContract_approval("是");
                    }else {
                        list1.setContract_approval("否");
                    }
                }

                //风险评估
                if (list1.getRisk_assessment()!=null){
                    if(list1.getRisk_assessment().equals("1")){
                        list1.setRisk_assessment("是");
                    }else {
                        list1.setRisk_assessment("否");
                    }
                }

                if(list1.getContract_type()!=null){
                    if(list1.getContract_type().equals("01")){
                        list1.setContract_type("普通合同");
                    }else if(list1.getContract_type().equals("02")){
                        list1.setContract_type("重大合同");
                    }else {
                        list1.setContract_type("特殊合同");
                    }
                }

            }
            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("重大经济合同台账管理", 7);
            String[] secondTitles = {"序号","合同编号", "合同名称","客户名称", "签订时间", "授权代表","合同工期开始时间"
                    ,"合同工期终止时间","执行单位","合同金额","付款方式及比例","合同评审","风险评估","备注","合同种类"};


            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            String[] beanProperty = { "contractId","contractNo" ,"title","othersideUnit", "cloneASignTime","authorized_representative","cloneDurationStartdate"
                    ,"cloneDurationEnddate","executive_unit","money","payment","contract_approval","risk_assessment","remarks","contract_type"};


            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "局重大合同台账.xls";
            filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename=" + filename);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }


    //编辑
    public ToJson<Contract> upContracts(HttpServletRequest request, Contracts contracts) {
        ToJson<Contract> json = new ToJson<Contract>(1, "error");
        try{
            contracts.getContract().setCreateTime(new Date());
            int count= contractsMapper.updateByPrimaryKeySelective(contracts.getContract());
            if(count>0){
                //合同的ID取出
                int contractId = contracts.getContract().getContractId();

                //相关流程
                if (contracts.getContractFlow()!=null && contracts.getContractFlow().size()>0){
                    //先删除后新增，因为条件判断是否修改过原来的
                    ContractFlowExample contractFlowExample=new ContractFlowExample();
                    ContractFlowExample.Criteria criteria = contractFlowExample.createCriteria();
                    criteria.andConTractIdEqualTo(contractId);
                    contractFlowsMapper.deleteByExample(contractFlowExample);
                    //新增
                    for (ContractFlow contractFlow:contracts.getContractFlow()){
                        contractFlow.setConTractId(contractId);
                        contractFlowsMapper.insertSelective(contractFlow);
                    }

                }

                //结算情况
                if (contracts.getContractProject()!=null && contracts.getContractProject().size()>0){
                    ContractProjectExample contractProjectExample=new ContractProjectExample();
                    ContractProjectExample.Criteria criteria = contractProjectExample.createCriteria();
                    criteria.andContractIdEqualTo(contractId);
                    contractProjectsMapper.deleteByExample(contractProjectExample);

                    for (ContractProject contractProject:contracts.getContractProject()){
                        contractProject.setContractId(contractId);
                        contractProjectsMapper.insertSelective(contractProject);
                    }
                }

                //  合同提醒 ,提醒是事务提醒，删除合同提醒表的同时也得删除sms和smsbody
                if (contracts.getContractRemind()!=null && contracts.getContractRemind().size()>0){
                    ContractRemindExample contractRemindExample=new ContractRemindExample();
                    ContractRemindExample.Criteria criteria = contractRemindExample.createCriteria();
                    criteria.andContractIdEqualTo(contractId);
                    contractRemindsMapper.deleteByExample(contractRemindExample);

                    //删除事务提醒
                    for (ContractRemind contractRemind:contracts.getContractRemind()){
                        contractRemind.setContractId(contractId);
                        contractRemindsMapper.insertSelective(contractRemind);
                    }
                }

                //判断  合同订单 不为空
                if (contracts.getContractTransport()!=null && contracts.getContractTransport().size()>0){
                    ContractTransportExample contractTransportExample=new ContractTransportExample();
                    ContractTransportExample.Criteria criteria = contractTransportExample.createCriteria();
                    criteria.andContractIdEqualTo(contractId);
                    contractTransportsMapper.deleteByExample(contractTransportExample);

                    for (ContractTransport contractTransport:contracts.getContractTransport()){
                        contractTransport.setContractId(contractId);
                        contractTransportsMapper.insertSelective(contractTransport);
                    }
                }
            }
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }
    public LimsJson selectContracts(HttpServletRequest request, Integer contractId) {
        LimsJson json = new LimsJson();
        try {
            List list = new ArrayList();
            //详情信息
            list.add(contractManageService.selectContractById(request, contractId));
            //相关流程
            list.add(contractManageService.selectContractFlowById(request,contractId));
            //订单
            list.add(contractManageService.selectContractTransportById(request, contractId));
            //提醒计划
            list.add(contractManageService.selectContractRemindById(request, contractId));
            //结算情况
            list.add(contractManageService.selectContractProjectById(request,contractId,"1"));
            json.setObj(list);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }

    /**
     *
     * @作者
     * @创建日期 13:54 2020/7/28
     * @类介绍 根据金额查询合同数据
     * @参数
     * @return limsJson
     */

    public LimsJson selectByMoney (HttpServletRequest request, Integer page, Integer limit,Integer money,Integer moneys){
        LimsJson limsJson =new LimsJson(1,"err");
        Map map = new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams",pageParams);
            map.put("money",money);
            map.put("moneys",moneys);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            BigDecimal summoney=new BigDecimal("0");
            if (users.getUserPriv() != 1){
                map.put("deptId",users.getDeptId());
            }
            if(money!=null&&moneys!=null){
                List<Contract> list =  contractsMapper.selectByMoney(map);
                for (Contract list1:list){
                    if (list1.getTypeId() != null){
                        ContractType contractType = contractTypesMapper.selectByPrimaryKey(list1.getTypeId());

                        list1.setTypeName(contractType !=null ? contractType.getTypeName() : "");
                    }

                    //总金额
                    summoney = summoney.add(list1.getMoney());
                }

                limsJson.setObject(summoney); //返回总金额
                limsJson.setData(list);
                PageParams pageParams =(PageParams) map.get("pageParams");
                limsJson.setCount(pageParams.getTotal());
                limsJson.setMsg("ok");
                limsJson.setCode(0);
            }else {
                map.put("money",money);
                List<Contract> list = contractsMapper.selectMoney(map);
                for (Contract list1:list){
                    if (list1.getTypeId() != null){
                        ContractType contractType = contractTypesMapper.selectByPrimaryKey(list1.getTypeId());

                        list1.setTypeName(contractType !=null ? contractType.getTypeName() : "");
                    }

                    //总金额
                    summoney = summoney.add(list1.getMoney());
                }

                limsJson.setObject(summoney); //返回总金额
                limsJson.setData(list);
                PageParams pageParams =(PageParams) map.get("pageParams");
                limsJson.setCount(pageParams.getTotal());
                limsJson.setMsg("ok");
                limsJson.setCode(0);
            }
        }catch(Exception e){
            limsJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return limsJson;
    }
    /**
     *
     * @作者
     * @创建日期 14:18 2020/7/28
     * @类介绍 查询重大合同类型数据
     * @参数
     * @return limsJson
     */

    public LimsJson selectBycontractType(HttpServletRequest request, Integer page, Integer limit,String contractType){
        LimsJson limsJson = new LimsJson(1,"err");
        Map map=new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams",pageParams);
            map.put("contractType",contractType);
        }catch (Exception e){
            e.printStackTrace();
        }

        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);

            if (users.getUserPriv() != 1){
                map.put("deptId",users.getDeptId());
            }
            List<Contract> list = contractsMapper.selectBycontractType(map);

            BigDecimal summoney=new BigDecimal("0");
            for (Contract list1:list){
                if (list1.getTypeId() != null){
                    ContractType contractTypes = contractTypesMapper.selectByPrimaryKey(list1.getTypeId());

                    list1.setTypeName(contractTypes !=null ? contractTypes.getTypeName() : "");
                }

                //总金额
                summoney = summoney.add(list1.getMoney());
            }

            limsJson.setObject(summoney); //返回总金额
            limsJson.setData(list);
            PageParams pageParams =(PageParams) map.get("pageParams");
            limsJson.setCount(pageParams.getTotal());
            limsJson.setMsg("ok");
            limsJson.setCode(0);
        } catch (Exception e) {
            limsJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return limsJson;
    }
}


